package com.booza.dao;

import java.util.Collection;
import java.util.Date;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.booza.constants.DatabaseConstants;
import com.booza.model.Financials;
import com.booza.util.PropertiesReader;

/**
 * DAO implementation class for financials table.
 */
@Transactional
public class FinancialsDAOImpl implements FinancialsDAO
{
	@Autowired
	private SessionFactory sessionFactory;

	@Autowired
	private PropertiesReader properties;
	
	public SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory = sessionFactory;
	}

	/**
	 * Find financials by primary key.
	 * 
	 * @param id
	 *          - PK
	 * @return matching financials row
	 */
	public Financials findFinancialsById(Integer id)
	{
		return (Financials) sessionFactory.getCurrentSession().get(DatabaseConstants.FINANCIALS_FQN, id);
	}

	/**
	 * Insert the current transaction details.
	 * 
	 * @param transaction
	 */
	@SuppressWarnings("unchecked")
	public Collection<Financials> findTransactionsForUser(Integer accountId)
	{
		return (Collection<Financials>) sessionFactory.getCurrentSession().createCriteria(Financials.class).add(
				Restrictions.eq("accountId", accountId)).list();
	}

	/**
	 * Find the list of all transactions of the given user.
	 * 
	 * @param accountId
	 *          - user id
	 * @return matching transactions
	 */
	public void insertFinancials(Financials transaction)
	{
		if (transaction.getTransactionDate() == null) {
			transaction.setTransactionDate(new Date());
		}
		sessionFactory.getCurrentSession().save(transaction);
	}

	/**
	 * Updates the financials row.
	 * 
	 * @param transaction
	 */
	public void updateFinancials(Financials transaction)
	{
		sessionFactory.getCurrentSession().saveOrUpdate(transaction);
	}

	/**
	 * Fetches list of all transaction in pending state.
	 * 
	 * @return collection of pending transactions
	 */
	@SuppressWarnings("unchecked")
	public Collection<Financials> findPendingTransactions()
	{
		String timeline = properties.getMassPayTimeLine();
		String query = "FROM Financials WHERE (status = 0 AND to_days(curdate())-to_days(trasactiondate) >= " + timeline + ")";
		return (Collection<Financials>) sessionFactory.getCurrentSession().createQuery(query).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Financials> findPendingDebitTransactions()
	{
		String timeline = properties.getMassPayTimeLine();
		String query = "FROM Financials WHERE (status = 0 AND creditordebit = 0 AND to_days(curdate())-to_days(trasactiondate) >= " + timeline + ")";
		return (Collection<Financials>) sessionFactory.getCurrentSession().createQuery(query).list();
	}	
}
